<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
	public function index(){
		$this->display('login');
	}
	public function Ulogin(){
		if(IS_POST){
	    $sele=I('sele','','htmlspecialchars');
		$email=I('email','','htmlspecialchars');
		$pass=I('password','','htmlspecialchars');
		$user=M('user')->where(array('user_email'=>$email))->find();
	
		if(!$user||$user['user_password']!=$pass){
              $this->error("账号密码错误");
		}
		else if($user['user_pass']==0){
			 $this->error("账号没激活！");
		    }else{
				//$Luser=M('user');
				$Luser['user_ip']=get_client_ip();//;
				$Luser['session_id']=session_id();//
				$succ=M('user')->where("user_id=$user[user_id]")->save($Luser);
			    //$Luser->where('user_id="$user[user_id]"')->save();
				session('header',$_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
				session('user_id',$user['user_id']);
				session('user_ip',$user['user_ip']);
				if(!$sele)
			    $this->redirect('Index/hall');	
				else{
					header("Location: $sele");
					}
			 }       
		}else{
			$this->error('页面不存在！');
		}
	
	}
	public function signup(){
		
		$yq=I('yq','','htmlspecialchars');
		if(empty($yq))$this->error('邀请链接不对哦！');
		$username=M('user')->where("user_linvite='$yq'")->field(array('user_id','user_name','user_img'))->find();
		if(empty($username)){$this->error('邀请链接不对哦！');}else{$this->assign('nickname',$username['user_name']);
		$this->assign('userId',$username['user_id']);
		}
        $tagParent=M('tag')->select();
		$arr=array();
	    function  tagLayer($cate,$pid=0){
			
			foreach($cate as $v){
				if($v['tag_parent_id']==$pid){
					$v['cate']=tagLayer($cate,$v['tag_id']);      
					$arr[]=$v;
					}
				}
				return $arr;
		}
		$tagList=tagLayer($tagParent);
		$this->assign('tag',$tagList);
		$this->display();
	}
				
	public function signup4(){
		if(IS_POST){
		$email=I('email','','htmlspecialchars');
		$pass=I('pass','','htmlspecialchars');
		$name=I('name','','htmlspecialchars');
		$yqlj=md5($email.'mzke');
		$jhm=md5($email).md5('mzke2014');
		$yqrId=I('yqrId','','int');
		if(empty($email)||empty($pass)||empty($name))$this->error('输入有误');
		  $date=array(
		  'user_email'=>$email,//默认已经过滤！
		  'user_password'=>$pass,
		  'user_name'=>$name,
		  'user_img'=>'/Public/user/image/2.png',
		  'user_time'=>time(),
		  'user_jhm'=>$jhm,
		  'user_linvite'=>$yqlj,
		  'user_ip'=>get_client_ip()
		  );
	      $user=M('user')->field(array('user_email','user_password','user_name','user_img','user_jhm','user_linvite','user_time','user_ip'))->data($date)->add();
		  if($user){
			  $arrSc=I('sc','','htmlspecialchars');
			  $arrXq=I('xq','','htmlspecialchars');
			  foreach($arrXq as $xq){
				  $dataXq[]=array(
				  'user_id'=>$user,
				  'tag_type'=>1,
				  'tag_id'=>$xq
				  ); 
				  } 
			  foreach($arrSc as $sc){
				  $dataSc[]=array(
				  'user_id'=>$user,
				  'tag_type'=>2,
				  'tag_id'=>$sc
				  );
				  }
				  
		  M('user_tag')->field('user_id','tag_type','tag_id')->addAll($dataSc);
		  M('user_tag')->field('user_id','tag_type','tag_id')->addAll($dataXq);
		  if(!$this->Email($email))$this->assign('email','邮箱发送失败请与管理员联系');
		  $linvite=M('user')->where("user_id=$yqrId")->getField('user_linvite_id');
		  $linvite=$linvite?$linvite.'@'.$user:$user;
		  M('user')->where("user_id=$yqrId")->setField('user_linvite_id',$linvite);
		  $this->assign('email',$email);
			  }else{
		 $this->error('注册失败！请于管理员联系');
				  }
	     $this->display(); 
		}else{$this->error("非法输入");}
	}			
		
   public function Email($email)
		{//$user=I('user'); $email=I('email'); 
		if(!$email)$this->error('非法访问');
		$jhm=md5($email).md5('mzke2014');
		header('Content-type:text/html;charset=utf-8');  //乱码问题
			 vendor("PHPMailer.class#phpmailer");//导出mail类库  
			 vendor("class.smtp#phpmailer");//导出mail类库        
			   $mail = new \PHPMailer();//实例化mail类
				  //你只需填写以下信息即可**************************** 
				$smtp = "smtp.exmail.qq.com";//必填，设置SMTP服务器 QQ邮箱是smtp.qq.com ，QQ邮箱默认未开启，请在邮箱里设置开通。网易的是 smtp.163.com 或 smtp.126.com
				
				$youremail =  'zhihui@imuuzi.com'; // 必填，开通SMTP服务的邮箱；也就是发件人Email。(本系统功能也就是自己给自己发邮件)
				
				$password = "mf674484212"; //必填， 以上邮箱对应的密码
				
				$ymail = "$email"; //收信人的邮箱地址，也就是你自己收邮件的邮箱
				
				$yname = "智慧公社"; //收件人称呼

//填写信息结束 ****************************
				$mail->IsSMTP();
				$mail->SMTPAuth = true; 
				$mail->Host = $smtp; 
				$mail->CharSet="utf-8";
				
				$mail->Username = $youremail; 
				$mail->Password = $password; //必填， 以上邮箱对应的密码
				
				$mail->From = $youremail; 
				$mail->FromName = "【智慧公社】"; 
				
				$mail->AddAddress($ymail,$yname);
	
	$mail->Subject = "【智慧公社账号激活】"; 
	date_default_timezone_set('Asia/Shanghai');
	$time = date("Y-m-d H:i:s",time());

	
	$html = '亲爱的智慧公社用户：<br />
<br />
感谢您参与内测，请点击以下链接激活账号http://zhihuigongshe.com'.U('Login/zhsuccess').'?success='.$jhm.'<br />
<br />
(如果您无法点击此链接，请将它复制到浏览器地址栏后访问)<br />
<br />
智慧公社团队<br />
'.date("Y-m-d H:i",time()).'<br />
<br />

此邮件为系统自动发出的邮件，请勿直接回复。<br />
<br />

';
	
	$mail->MsgHTML($html);
	
	$mail->IsHTML(true); 
	
	if($mail->Send()){return true;}else{return false;}
	}
   public function zhsuccess(){
		$jhm=I('success','','htmlspecialchars');
		$where['user_jhm']=array('EQ',$jhm);
		if($list=M('user')->where($where)->find()){
		if($list['user_pass']==1)$this->redirect('Index/index','',4,'您已经验证过了，去首页看看吧！');
			$data['user_pass']='1';
			$where1['user_id']=array('EQ',$list['user_id']);
			if($ass=M('user')->where($where)->data($data)->save())
			$this->display();
			
		  }
		 else{$this->error('验证失败');}
		
				
			
					
					
		   }

}
?>